package org.tgcloud.group.web.controller.manager;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.core.custom.Permissions;
import org.tgcloud.group.core.session.Session;
import org.tgcloud.group.service.manager.ManagerUserService;
import org.tgcloud.group.web.controller.dto.user.ManagerAdminUserDTO;

import java.util.Date;

/**
 * @author: zhy
 * @create: 2023-07-24 13:39
 * @description: 管理员相关接口
 */
@Api(tags = "用户管理相关接口")
@RestController
@RequestMapping("/manager/pc/user")
@EnableAutoConfiguration
public class ManagerUserController extends ManageAPPBaseController{

    @Autowired
    private ManagerUserService managerUserService;

//    @Permissions("managerUser:view")
    @ApiOperation(value = "管理员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @GetMapping(value = "/adminUserList")
    public APPResultMessage<Object, ManagerAdminUserDTO> getAdminUserList(@ModelAttribute("session") Session session,
                                                                          @RequestParam(required = false) Integer pageNum,
                                                                          @RequestParam(required = false) Integer pageSize,
                                                                          @RequestParam(required = false) String keyword,
                                                                          @RequestParam(required = false) Long startTime,
                                                                          @RequestParam(required = false) Long endTime,
                                                                          @RequestParam(required = false) String roleId,
                                                                          @RequestParam(required = false) Short status) {
        return managerUserService.getAdminUserList(pageNum,pageSize,keyword,startTime == null ? null : new Date(startTime),
                endTime == null ? null : new Date(endTime), roleId, status);
    }


    @Permissions("managerUser:add")
    @ApiOperation(value = "添加管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @PostMapping(value = "/doAddAdminUser")
    public APPResultMessage doAddAdminUser(@ModelAttribute("session") Session session,
                                           @RequestParam String password,
                                           @RequestParam String username,
                                           @RequestParam String roleId) {
        return managerUserService.doAddAdminUser(password,username,roleId);
    }

    @ApiOperation(value = "查看自己账号")
    @ResponseBody
    @RequestMapping(value = "/getManagerUserMsg", method = RequestMethod.POST)
    public APPResultMessage getManagerUserMsg(@ModelAttribute("session") Session session) {
        return managerUserService.getManagerUserMsg(session.getUserId());
    }

    @Permissions("managerUser:edit")
    @ApiOperation(value = "修改管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @PostMapping(value = "/doUpAdminUser")
    public APPResultMessage doUpAdminUser(@ModelAttribute("session") Session session,
                                          @RequestParam int userId,
                                          @RequestParam(required = false) String username,
                                          @RequestParam(required = false) String roleId,
                                          @RequestParam(required = false) String password) {
        return managerUserService.doUpAdminUser(userId,username,roleId,password);
    }

    @Permissions("managerUser:editPassword")
    @ApiOperation(value = "修改管理员密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @PostMapping(value = "/doUpAdminUserPassword")
    public APPResultMessage doUpAdminUserPassword(@ModelAttribute("session") Session session,
                                                  @RequestParam int userId,
                                                  @RequestParam String password) {
        return managerUserService.doUpAdminUserPassword(userId,password);
    }

    @Permissions("managerUser:editStatus")
    @ApiOperation(value = "修改管理员状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @PostMapping(value = "/doUpAdminUserStatus")
    public APPResultMessage doUpAdminUserStatus(@ModelAttribute("session") Session session,
                                                @RequestParam String userIds,
                                                @RequestParam short status) {
        if (StringUtil.isBlank(userIds)){
            return APPResultMessage.MISS_PARAMETER();
        }
        return managerUserService.doUpAdminUserStatus(userIds,status);
    }


    @ApiOperation(value = "修改最后登录时间")
    @ResponseBody
    @RequestMapping(value = "/updateUserLastTime", method = RequestMethod.POST)
    public APPResultMessage doUpdateUserLastTime(@ModelAttribute("session") Session session) {
        return managerUserService.doUpdateUserLastTime(session.getUserId());
    }


    /**
     * @author: zhy
     * @description: 绑定小程序用户
     * @create: 2023年08月14日 16:43
     * @return
     */
    @Permissions("managerUser:bind")
    @ApiOperation(value = "绑定小程序用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @PostMapping(value = "/doBindNormalUser")
    public APPResultMessage doBindNormalUser(@ModelAttribute("session") Session session,
                                                @RequestParam Integer userId,
                                                @RequestParam Integer appUserId) {
        return managerUserService.doBindNormalUser(userId,appUserId);
    }


    @ApiOperation(value = "根据角色获取管理员用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @GetMapping(value = "/getChooseAdminUserList")
    public APPResultMessage getChooseAdminUserList(@ModelAttribute("session") Session session,
                                                                          @RequestParam(required = false) String roleId) {
        return managerUserService.getChooseAdminUserList(roleId);
    }


}
